﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.Drawing;

namespace syc.ZhiJian.Report
{
    public partial class 发货明细 : System.Web.UI.Page
    {
        protected string year;
        protected string month;
        protected string RiQiStart;
        protected string RiQiEnd;
        protected string RiQi;


        /// <summary>
        /// 读入所有的SESSION数据，向VIEW暴露数据项
        /// </summary>
        protected void read_session()
        {
            year = DList_year.SelectedValue;
            month = DList_month.SelectedValue;
        }

        private string sql = @"
SELECT dbo.产品出库单.日期, dbo.产品订货单位.名称, 
       dbo.产品.品名, COUNT(*) AS 车数, 
       SUM(dbo.产品出库单.总件数) AS 捆数, 
       SUM(dbo.产品出库单.总支数) AS 支数, 
       SUM(dbo.产品出库单.总重量) AS 总重量, 
       SUM(dbo.产品出库单.总长度) AS 总长度
FROM   dbo.产品出库单 INNER JOIN
       dbo.产品销售合同 ON dbo.产品出库单.合同ID = dbo.产品销售合同.ID INNER JOIN
       dbo.产品订货单位 ON dbo.产品销售合同.订货单位ID = dbo.产品订货单位.ID INNER JOIN
       dbo.产品 ON dbo.产品出库单.产品类别ID = dbo.产品.ID
WHERE (dbo.产品出库单.产品类别ID=*产品*) AND
      (MONTH(dbo.产品出库单.日期)=*月份*) AND
      (YEAR(dbo.产品出库单.日期)=*年份*)
GROUP BY dbo.产品出库单.日期, dbo.产品订货单位.名称, dbo.产品.品名";
        protected static DateTime date;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";     
            
            if (!this.IsPostBack)
            {
                DList_year.SelectedValue = DateTime.Now.Year.ToString();
                DList_month.SelectedValue = DateTime.Now.Month.ToString();
                sql = sql.Replace("*年份*", DList_year.SelectedValue);
                sql = sql.Replace("*月份*", DList_month.SelectedValue);
                sql = sql.Replace("*产品*",DList_Produce.SelectedValue);
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            read_session();
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];

            //这一段分组的代码逻辑会强制执行，所以无需再在各个事件中重写
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("名称", true, true);
            helper.RegisterSummary("车数", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("捆数", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("支数", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("总重量", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("总长度", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("车数", SummaryOperation.Sum);
            helper.RegisterSummary("捆数", SummaryOperation.Sum);
            helper.RegisterSummary("支数", SummaryOperation.Sum);
            helper.RegisterSummary("总重量", SummaryOperation.Sum);
            helper.RegisterSummary("总长度", SummaryOperation.Sum);
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
        }


        //自定义分组标题样式
        private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            row.Cells[0].Height = 33;
            row.BackColor = Color.LightGray;
            row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;

        }

        //自定义合计标题样式
        private void helper_GeneralSummary(GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.BackColor = Color.LightSlateGray;
            row.Cells[0].Text = "总计:";
            row.Height = 40;
        }


        //自定义合计标题样式
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.Cells[0].Text = "小计:";
            row.Height = 40;
        }

        protected void DList_year_SelectedIndexChanged(object sender, EventArgs e)
        {
                sql = sql.Replace("*年份*", DList_year.SelectedValue);
                sql = sql.Replace("*月份*", DList_month.SelectedValue);
                sql = sql.Replace("*产品*", DList_Produce.SelectedValue);
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        protected void DList_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            sql = sql.Replace("*年份*", DList_year.SelectedValue);
            sql = sql.Replace("*月份*", DList_month.SelectedValue);
            sql = sql.Replace("*产品*", DList_Produce.SelectedValue);
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

    }
}
